<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :gutter="20">
        <!--模具数据-->
        <div class="head-container">
          <el-tree
            :data="deviceTypeList"
            :props="defaultProps"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['device:preserveDescribe:add']"
              >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['device:preserveDescribe:edit']"
              >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['device:preserveDescribe:remove']"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport('#preserveDescribe', '保全项目描述')"
              v-hasPermi="['device:preserveDescribe:export']"
              >导出
            </el-button>
          </el-col>

          <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
            <Page
              style="margin-right: 0px"
              :current="queryParams.pageNum"
              :page-size="queryParams.pageSize"
              :total="total"
              @on-change="pageChange"
              simple
            ></Page>
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
        <el-table
          v-loading="loading"
          :data="preserveDescribeList"
          :height="tableH"
          border
          stripe
          id="preserveDescribe"
          highlight-current-row
          :header-cell-style="{ 'text-align': 'center' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="#"
            fixed="left"
            type="index"
            width="55"
            align="center"
          />
          <el-table-column
            type="selection"
            fixed="left"
            width="55"
            align="center"
          />
          <el-table-column
            label="序号"
            width="55"
            align="center"
            prop="bnlSn"
          />
          <el-table-column label="保全分类" width="120" prop="bnlNrlb" />
          <el-table-column
            label="保全项目描述"
            show-overflow-tooltip
            width="400px"
            prop="bnlNrms"
          />
          <el-table-column
            label="有效标识"
            align="center"
            width="120"
            key="bnlFlag"
          >
            <template #default="scope">
              <el-switch
                v-model="scope.row.bnlFlag"
                active-value="0"
                inactive-value="1"
                @change="changeBnlFlag(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            show-overflow-tooltip
            width="200"
            prop="bnlDesc"
          />
          <el-table-column
            label="建立人员"
            align="center"
            width="100"
            prop="bnlJlry"
          />
          <el-table-column
            label="建立日期"
            align="center"
            prop="bnlJlrq"
            width="150"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.bnlJlrq, "{y}-{m}-{d} {h}:{m}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="更改人员"
            align="center"
            width="100"
            prop="bnlGgry"
          />
          <el-table-column
            label="更改日期"
            align="center"
            prop="bnlGgrq"
            width="150"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.bnlGgrq, "{y}-{m}-{d} {h}:{m}:{s}")
              }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="100px"
            class-name="small-padding fixed-width"
          >
            <template v-slot="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['device:preserveDescribe:edit']"
                >修改
              </el-button>

              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['device:preserveDescribe:remove']"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 添加或修改设备类型对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="600px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="设备类型" prop="bnlCode">
            <el-input v-model="form.bnlCode" placeholder="请输入设备类型" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="序号" prop="bnlSn">
            <el-input
              v-model="form.bnlSn"
              placeholder="请输入序号，序号不能与类型中已有的序号重复..."
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="有效标志">
            <el-radio-group v-model="form.bnlFlag">
              <el-radio
                v-for="dict in flagOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="保全分类" prop="bnlNrlb">
            <el-input v-model="form.bnlNrlb" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="检测项目" prop="bnlNrms">
            <el-input type="textarea" placeholder="必填" v-model="form.bnlNrms">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="bnlDesc">
            <el-input v-model="form.bnlDesc" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addPreserveDescribe,
  changedPreserveDescribeFlag,
  deletePreserveDescribe,
  editPreserveDescribe,
  exportPreserveDescribe,
  getByIdPreserveDescribe,
  listEffectiveDeviceType,
  listPreserveDescribe,
} from "@/api/device/preserveDescribe";

import { exportTable } from "@/utils/ruoyi";

export default {
  name: "PreserveDescribe",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      tableH: document.body.clientHeight > 900 ? 645 : 480,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备类型表格数据
      deviceTypeList: [],

      //保全项目描述
      preserveDescribeList: [],

      //保存父属性
      defaultProps: {
        children: "children",
        label: "label",
      },

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 有效标识字典
      flagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 18,
        bnlCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bnlCode: [
          { required: true, message: "设备类型代码不能为空", trigger: "blur" },
        ],
        bnlSn: [{ required: true, message: "序号不能为空", trigger: "blur" }],
        bnlNrlb: [
          { required: true, message: "保全分类不能为空", trigger: "blur" },
        ],
        bnlNrms: [
          { required: true, message: "检测项目必须填写", trigger: "blur" },
        ],
      },
    };
  },
  // 初始化获取表单
  created() {
    this.getTreeSelect();
    this.getList();
    this.getDicts("sys_flag").then((response) => {
      this.flagOptions = response.data;
    });
  },
  methods: {
    /** 查询设备类型列表 */
    getList() {
      this.loading = true;
      listPreserveDescribe(this.queryParams).then((response) => {
        this.preserveDescribeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    getTreeSelect() {
      this.deviceTypeList = [];
      listEffectiveDeviceType().then((response) => {
        this.deviceTypeList.push(response.data);
      });
    },

    // 表单重置
    reset() {
      this.form = {
        bnlSn: null,
        bnlNrms: null,
        bnlNrlb: null,
        bnlFlag: "0",
        bnlDesc: null,
        bnlJlry: null,
        bnlJlrq: null,
        bnlGgry: null,
        bnlGgrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.bnlCode = this.queryParams.bnlCode;
      this.open = true;
      this.title = "添加设备类型";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bnlId = row.bnlId || this.ids;
      getByIdPreserveDescribe(bnlId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备类型";
      });
    },

    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bnlId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 按钮改变事件 */
    changeBnlFlag(row) {
      let text = row.bnlFlag === "0" ? "启用" : "停用";
      this.$confirm("确认要" + text + "吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return changedPreserveDescribeFlag(row.bnlId, row.bnlFlag);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.bnlFlag = row.bnlFlag === "0" ? "1" : "0";
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bnlId != null) {
            editPreserveDescribe(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPreserveDescribe(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 设备保全单击事件 */
    handleNodeClick(data) {
      this.queryParams.bnlCode = data.property;
      this.getList();
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const bnlId = row.bnlId || this.ids;
      this.$confirm("是否确认删除此数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deletePreserveDescribe(bnlId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },

    handleExport(a, b) {
      exportTable(a, b);
    },
  },
};
</script>
